

jQuery(function ($) {
    //四级联动
    //新增地址
    var $this = $("#area");
    var $province = $this.find('select[name="province"]'),
        $city = $this.find('select[name="city"]'),
        $area = $this.find('select[name="area"]'),
        $town = $this.find('select[name="town"]');
    var params = {
        "Area[]": {
            "count": 0,
            "Area": {
                "use_flag": 1,
                "level": 1
            }
        }
    };
    send_ajax('post', '/get', params, function callback(result) {
        if (result.code == 200) {
            result = result["Area[]"];
        };
        var formatProvince = [],
            provinceField = [],
            currentProvince = null,
            cityField = [],
            currentCity = null,
            areaField = [],
            currentArea = null,
            townField = [];
        formatProvince = result;
        formatProvince.map(function (value, index) {
            provinceField.push(value.name);
        });
        var format = {
            province: function province() {
                $province.empty();
                $province.append('<option value=""> - 请选择 - </option>');
                $province.prop('disabled', provinceField.length === 0);
                for (var i in provinceField) {
                    $province.append('<option value="' + i + '" data-code="' + i + '">' + provinceField[i] + '</option>');
                }
                this.city();
            },
            city: function city() {
                $city.empty();
                $city.append('<option value=""> - 请选择 - </option>');
                $city.prop('disabled', cityField.length === 0);
                $city.css('display', cityField.length === 0 ? "none" : "inline-block");
                for (var i in cityField) {
                    $city.append('<option value="' + i + '" data-code="' + i + '">' + cityField[i] + '</option>');
                }
                this.area();
            },
            area: function area() {
                $area.empty();
                $area.append('<option value=""> - 请选择 - </option>');
                $area.prop('disabled', areaField.length === 0);
                $area.css('display', areaField.length === 0 ? "none" : "inline-block");
                for (var i in areaField) {
                    $area.append('<option value="' + i + '" data-code="' + i + '">' + areaField[i] + '</option>');
                }
                this.town();
            },
            town: function town() {
                $town.empty();
                $town.append('<option value=""> - 请选择 - </option>');
                $town.prop('disabled', townField.length === 0);
                $town.css('display', townField.length === 0 ? "none" : "inline-block");
                for (var i in townField) {
                    $town.append('<option value="' + i + '" data-code="' + i + '">' + townField[i] + '</option>');
                }
            }

        };

        format.province();
        //事件绑定
        $province.on('change', function () {
            $('.city').css('display', 'inline-block');
            isLinkage = true;
            currentProvince = null, cityField = [], cityId = 0, currentCity = null, areaField = [], areaId = 0, currentArea = null, townField = [], townId = 0;
            currentProvince = formatProvince[$province.find('option:selected').attr('data-code')];
            provinceId = currentProvince.id;
            var params = {
                "Area[]": {
                    "count": 0,
                    "Area": {
                        "use_flag": 1,
                        "parent_id": provinceId
                    }
                }

            };
            send_ajax('post', '/get', params, function callback(result) {
                if (result.code == 200) {
                    result = result["Area[]"];
                    city_list = result;
                };
                city_list.map(function (value, index) {
                    cityField.push(value.name);
                });
                format.city();
            });
        });
        $city.on('change', function () {
            $('.area').css('display', 'inline-block');
            currentCity = null, areaField = [], areaId = 0, currentArea = null, townField = [], townId = 0;
            currentCity = city_list[$city.find('option:selected').attr('data-code')];
            cityId = currentCity.id;
            var params = {
                "Area[]": {
                    "count": 0,
                    "Area": {
                        "use_flag": 1,
                        "parent_id": cityId
                    }
                }
            };
            send_ajax('post', '/get', params, function callback(result) {
                if (result.code == 200) {
                    result = result["Area[]"];
                    county_list = result;
                }
                // areaId=currentCity.county_list[0].area_id;
                county_list.map(function (value, index) {
                    areaField.push(value.name);
                });
                format.area();
            });
        });
        $area.on('change', function () {
            areaId = 0, currentArea = null, townField = [], townId = 0;
            currentArea = county_list[$area.find('option:selected').attr('data-code')];
            areaId = currentArea.id;
            var params = {
                "Area[]": {
                    "count": 0,
                    "Area": {
                        "use_flag": 1,
                        "parent_id": areaId
                    }
                }
            };
            send_ajax('post', '/get', params, function callback(result) {
                if (result.code == 200) {
                    result = result["Area[]"];
                    town_list = result;
                };
                // townId =currentArea.town_list[0].area_id;
                if (town_list) {
                    $('.town').css('display', 'inline-block');
                    town_list.map(function (value, index) {
                        townField.push(value.name);
                    });
                }
                format.town();
            });
        });
        $town.on('change', function () {
            townId = town_list[$town.find('option:selected').attr('data-code')].id;
        });
    });
});